﻿using JV.Com.Business;
using JV.Com.Converters;
using JV.Com.Internals;
using JV.Com.Model;
using Microsoft.Practices.Prism.StoreApps;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

namespace JV.Com.ViewModels.Wrapper
{
    public abstract class GameArticleWrapper
    {
        public static GameArticleWrapper Create(PageBaseViewModel parent, Article article)
        {
            switch (article.Type)
            {
                case ArticleType.Test: return new TestItemWrapper(parent, article);
                case ArticleType.Folder: return new FolderItemWrapper(parent, article);
                case ArticleType.Preview: return new PreviewItemWrapper(parent, article);
                default: throw new NotSupportedException();
            }
        }

        protected readonly PageBaseViewModel m_parent;
        protected readonly Article m_article;

        public GameArticleWrapper(PageBaseViewModel parent, Article article)
        {
            m_parent = parent;
            m_article = article;
        }

        public int Id { get { return m_article.Id; } }
        public string Title { get { return m_article.Title; } }

        public abstract string Summary { get; }
        public ArticleType Type { get { return m_article.Type; } }
        public Article Article { get { return m_article; } }
        public string SmallImageUrl { get { return m_article.SmallImageUrl; } }
        public abstract string FormattedType { get; }
        public IEnumerable<byte[]> Machines
        {
            get
            {
                foreach (var machine in m_article.Machines)
                {
                    if (Cache.Instance.Machines.ContainsKey(machine))
                    {
                        var bytes = Cache.Instance.Machines[machine].Image;
                        yield return bytes;
                    }
                }
            }
        }
        public abstract void OnClick();
    }

    public class TestItemWrapper : GameArticleWrapper
    {
        public TestItemWrapper(PageBaseViewModel parent, Article article)
            : base(parent, article)
        {
        }

        public int Rating { get { return ((Test)m_article).Rating; } }

        public override string FormattedType { get { return "Test"; } }

        public override void OnClick() { m_parent.NavigationService.Navigate("TestGame", m_article.Id); }

        public override string Summary
        {
            get
            {
                return string.Format("{0} / 20", ((Test)m_article).Rating);
            }
        }
    }

    public class PreviewItemWrapper : GameArticleWrapper
    {
        public PreviewItemWrapper(PageBaseViewModel parent, Article article)
            : base(parent, article)
        {
        }

        public override string FormattedType { get { return "Aperçu"; } }
        public PreviewRating Rating { get { return ((Preview)m_article).Rating; } }

        public override void OnClick() { m_parent.NavigationService.Navigate("PreviewGame", m_article.Id); }

        public override string Summary
        {
            get
            {
                return (string)new PreviewRatingToDisplayTextConverter().Convert(((Preview)m_article).Rating, null, null, null);
            }
        }
    }

    public class FolderItemWrapper : GameArticleWrapper
    {
        public FolderItemWrapper(PageBaseViewModel parent, Article article)
            : base(parent, article)
        {
        }

        public override string FormattedType { get { return "Dossier"; } }

        public override void OnClick() { m_parent.NavigationService.Navigate("Folder", m_article.Id); }

        public override string Summary
        {
            get { return m_article.Date.ToString("dd/MM/yyyy"); }
        }
    }
}